package com.company.emenu.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页对象. 包含当前页数据及分页信息如总记录数.
 * 
 * @author shizh
 */
@SuppressWarnings("serial")
public class Page implements Serializable {

    public static int DEFAULT_PAGE_SIZE = 12;

    private int        pageSize          = DEFAULT_PAGE_SIZE; // 每页的记录数

    private int        start;                                // 当前页第一条数据在List中的位置,从0开始

    private int        totalCount        = 0;                // 总记录数
    
    private int		   currentPage       = 1	            ;//当前页
    
    private int		   maxPage			 = 1				;//最大页
    
    protected List<Integer> pagelist = new ArrayList<Integer>();
    
    private Map pageMap = new HashMap(); 
    

    /**
     * 构造方法.
     */
    public Page()
    {
        this(0, DEFAULT_PAGE_SIZE);
    }
    
    public List getPagelist() {
		return pagelist;
	}

	public void setPagelist() {
		pagelist = new ArrayList<Integer>();
		int start = 0;
		int k = 0;
		if (this.maxPage <= 10) {
			start = 1;
			k = this.maxPage;
		} else {
			start = this.currentPage - 3;
			if (start < 1)
				start = 1;
			if (start + 10 > this.maxPage)
				start = this.maxPage - 10 + 1;
			k = 10;

		}
		for (int i = start; i < start + k; i++) {
			pagelist.add(i);
		}
	}

    /**
     * 默认构造方法.
     * 
     * @param start
     *            本页数据在数据库中的起始位置
     * @param pageSize
     *            本页容量
     */
    public Page(int start, int pageSize)
    {
        this.start = start;
        this.pageSize = pageSize;

    }

    /**
     * 设置总记录数
     */
    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
        this.maxPage = getTotalPageCount();
    }

    /**
     * 取总记录数.
     */
    public int getTotalCount()
    {
        return this.totalCount;
    }

    /**
     * 设置起始索引
     */
    public void setStart(int start)
    {
        this.start = start;
    }

    /**
     * 获取起始索引
     
    public int getStart()
    {
        return start;
    }
	*/

    /**
     * 取总页数.
     */
    public int getTotalPageCount()
    {
        if (totalCount % pageSize == 0)
            return totalCount / pageSize;
        else
            return totalCount / pageSize + 1;
    }
    /**
     * 取每页数据容量.
     */
    public int getPageSize()
    {
        if (pageSize == 0)
            pageSize = DEFAULT_PAGE_SIZE;
        return pageSize;
    }

    /**
     * 取该页当前页码,页码从1开始.
     */
    public int getCurrentPageNo()
    {
        return start / pageSize + 1;
    }

    /**
     * 该页是否有下一页.
     */
    public boolean hasNextPage()
    {
        return this.getCurrentPageNo() < this.getTotalPageCount() - 1;
    }

    /**
     * 该页是否有上一页.
     */
    public boolean hasPreviousPage()
    {
        return this.getCurrentPageNo() > 1;
    }

    /**
     * 获取任一页第一条数据在数据集的位置，每页条数使用默认值.
     * 
     * @see #getStartOfPage(int,int)
     */
    protected static int getStartOfPage(int pageNo)
    {
        return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
    }

    /**
     * 获取任一页第一条数据在数据集的位置.
     * 
     * @param pageNo
     *            从1开始的页号
     * @param pageSize
     *            每页记录条数
     * @return 该页第一条数据
     */
    public static int getStartOfPage(int pageNo, int pageSize)
    {
        return (pageNo - 1) * pageSize;
    }

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public int getStart()
	{
		return (currentPage - 1) * pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	@SuppressWarnings("unchecked")
	public Map getPageMap() {
		pageMap.put("CurrentPage",String.valueOf(currentPage));
		pageMap.put("MaxPage", String.valueOf(maxPage));
		pageMap.put("TotalCount", String.valueOf(totalCount));
		return pageMap;
	}

    
    
}